package com.king.headline.UI;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.king.headline.R;
import com.king.headline.adapter.MainAdapter;
import com.king.headline.base.BaseActivity;
import com.king.headline.beans.MainInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.Configuration;
import com.king.headline.util.CustomToast;
import com.king.headline.util.MyTextUtils;
import com.king.headline.util.UIUtils;
import com.king.headline.view.CircleImageView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by king on 2016/6/26.
 */

public class UserActivity extends BaseActivity {
    @BindView(R.id.lv_main)
    PullToRefreshListView mRlListView;
    @BindView(R.id.tv_sourceDetail)
    TextView tv_sourceDetail;
    @BindView(R.id.tv_sourceName)
    TextView tv_sourceName;
    @BindView(R.id.iv_chatHeadurl)
    CircleImageView iv_chatHeadurl;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    private String sourceid;
    private ListView lv_main;
    private boolean isRefresh;
    private boolean isLoadMore;
    private int index;
    private  ArrayList<MainInfo> mainInfoListAll = new ArrayList<MainInfo>();
    private MainAdapter adapter;
    private MyTextUtils myTextUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.addActivity(this);
        baseSetContentView(R.layout.activity_user);
        myTextUtils = new MyTextUtils();
        sourceid = getIntent().getStringExtra("sourceid");
        setTopBarAndAction();
        initView();
        getMainFromNet();
    }

    private void initView() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv_main = mRlListView.getRefreshableView();
        mRlListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRlListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                isRefresh = true;
                mainInfoListAll.clear();
                index = 0;// 页面重置
                getMainFromNet();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                isLoadMore = true;
                if (mainInfoListAll != null && mainInfoListAll.size() > 0) {
                    index = mainInfoListAll.get(mainInfoListAll.size() - 1).index_;
                }
                getMainFromNet();
            }

        });
    }

    private void setTopBarAndAction() {
        setBarTitleText(""); // 设置Title标题
        setBackIcon(true);
    }

    public void getMainFromNet() {
        NetWork.getInstance().getUserPage(sourceid, index, new Callback() {
            @Override
            public void success(String result) {
                RefreshComplete();
                setAppMain(result);
            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                RefreshComplete();
                CustomToast.showToast(message);
            }

            @Override
            public void failed(String e) {
                RefreshComplete();
                CustomToast.showToast(e);
            }
        });

    }
    private void setAppMain(String result){
        ArrayList<MainInfo> mainInfoList = new Gson().fromJson(
                result,
                new TypeToken<List<MainInfo>>() {
                }.getType());
        if (mainInfoList == null || mainInfoList.size() == 0) {
            CustomToast.showToast("没有更多数据了");
        } else {
            mainInfoListAll.addAll(mainInfoList);
            if (adapter == null) {
                adapter = new MainAdapter(this, mainInfoListAll);
                adapter.isClickUserPage(false);
                lv_main.setAdapter(adapter);
            } else {
                adapter.setDate(mainInfoListAll);
            }
            myTextUtils.setText(tv_sourceDetail,mainInfoListAll.get(0).source.qianming);
            myTextUtils.setText(tv_sourceName,mainInfoListAll.get(0).source.username);
            UIUtils.getImageLoader().displayImage(Configuration.API_HOST+mainInfoListAll.get(0).source.chatHeadurl,iv_chatHeadurl);
        }
    }
    private void RefreshComplete(){
        if (mRlListView != null) {
            mRlListView.onRefreshComplete();
        }
        isRefresh=false;
        isLoadMore=false;
    }

}
